<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreUpload {
	var $file;
	var $filename;
	var $filetype;
	var $filesize;
	var $fileerror;
	var $tmp_filename;
	var $save_filename;
	var $save_file;
	var $extension;
	
	var $destination;
	var $permissions;
	var $extensions;
	var $types;
	
	var $maxsize = 0;
	var $message_location;
	
	var $destination_type = 'random';
	var $destination_subdir;
	var $rand_filename = true;
	
	//constructor
	function CoreUpload($file = '', $destination = '', $permissions = '777', $extensions = '') {
		$this->setFile($file);
		//$this->parseExtension();
		
		$this->setDestination($destination);
		$this->setPermissions($permissions);
		$this->setExtensions($extensions);
		
		$this->setOutputMessages('direct');
		
		if( $this->_not_null($this->file) && $this->_not_null($this->destination)) {
			$this->setOutputMessages('session');
			
			if( ($this->parse() == true) && ($this->save() == true) ) {
				return true;
			} else {
				return false;
			}
		}
	}
	
	//methods
	function parse() {
		$has_error = false;
		if( isset($_FILES[$this->file]) ) {
			$file = array('name' => $_FILES[$this->file]['name'],
						'type' => $_FILES[$this->file]['type'],
						'size' => $_FILES[$this->file]['size'],
						'tmp_name' => $_FILES[$this->file]['tmp_name']);
		} elseif( isset($GLOBALS['HTTP_POST_FILES'][$this->file]) ) {
			global $HTTP_POST_FILES;
		
			$file = array('name' => $HTTP_POST_FILES[$this->file]['name'],
						'type' => $HTTP_POST_FILES[$this->file]['type'],
						'size' => $HTTP_POST_FILES[$this->file]['size'],
						'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']);
		} else {
			$file = array('name' => (isset($GLOBALS[$this->file . '_name']) ? $GLOBALS[$this->file . '_name'] : ''),
						'type' => (isset($GLOBALS[$this->file . '_type']) ? $GLOBALS[$this->file . '_type'] : ''),
						'size' => (isset($GLOBALS[$this->file . '_size']) ? $GLOBALS[$this->file . '_size'] : ''),
						'tmp_name' => (isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] : ''));
		}
		
		if( $this->_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) {
			if( !empty($this->extensions) && !in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions)) {
				$this->_ms_add('top', 'filetype_not_allowed', 'error');
				return false;
			}
			
			$this->setFile($file);
			$this->setFilename($file['name']);
			$this->parseExtension();
			$this->setFiletype($file['type']);
			$this->setTmpFilename($file['tmp_name']);
			
			if( $this->checkFile($file) == false ) {
				$has_error = true;
			}
		} else {
			$this->_ms_add('top', 'no_file_uploaded', 'warning');
			$has_error = true;
		}
		if( $has_error == true ) {
			return false;
		}
		return $this->checkDestination();
	}
	
	function save() {
		if( substr($this->destination, -1) != '/' ) {
			$this->destination .= '/';
		}
		
		if( $this->rand_filename == true || strlen($this->filename) == 0 ) {
			$this->save_filename = $this->_generate_filename();
		} else {
			//$this->save_filename = $this->filename;
			$this->save_filename = $this->_filter_filename($this->filename);
		}
		if( strlen($this->destination_subdir) > 0 ) {
			$this->save_filename = $this->destination_subdir . '/' . $this->save_filename;
		}
		$savepath = $this->destination . $this->save_filename;
		
		if( move_uploaded_file($this->file['tmp_name'], $savepath)) {
			chmod($savepath, $this->permissions);
			
			$this->save_file = $savepath;
			$this->_ms_add('top', 'file_saved_success', 'success');
			
			return true;
		} else {
			$this->_ms_add('top', 'file_not_saved', 'error');
			
			return false;
		}
	}
	
	function upload() {
		if( ($this->parse() == true) && ($this->save() == true) ) {
			return true;
		} else {
			return false;
		}
	}
	
	//
	function checkFile($file) {
		$extension = strtolower(substr($file['name'], strrpos($file['name'], '.')+1));
		$type = $file['type'];
		$size = $file['size'];
		if( $this->checkFileExt($extension) == false || $this->checkFileType($type) == false || $this->checkFileSize($size) == false ) {
			return false;
		}
		return true;
	}
	
	function checkFileExt($extension) {
		if( !empty($this->extensions) && !in_array(strtolower($extension), $this->extensions)) {
			$this->_ms_add('top', 'filetype_not_allowed', 'error');
			return false;
		}
		return true;
	}
	
	function checkFileType($type) {
		if( !empty($this->types) && !in_array(strtolower($type), $this->types) ) {
			$this->_ms_add('top', 'filetype_not_allowed', 'error');
			return false;
		}
		return true;
	}
	
	function checkFileSize($size) {
		if( $this->maxsize > 0 && $this->maxsize < $size ) {
			$this->_ms_add('top', 'filesize_not_allowed', 'error');
			return false;
		}
		return true;
	}
	
	function checkFileUploadable($file) {
		return is_uploaded_file($file);
	}
	
	function checkDestination() {
		$has_error = false;
		if( !is_writable($this->destination) ) {
			if( is_dir($this->destination) ) {
				$this->_ms_add('top', array('destination_not_writable', $this->destination), 'error');
			} else {
				$this->_ms_add('top', array('destination_not_exist', $this->destination), 'error');
			}
			$has_error = true;
		} else {
			$this->destination_subdir = '';
			if( $this->destination_type == 'month' ) {
				$this->destination_subdir = strftime("%Y%m", time());
			} elseif( $this->destination_type == 'day' ) {
				$this->destination_subdir = strftime("%Y%m%d", time());
			} elseif( $this->destination_type == 'random' ) {
				$this->destination_subdir = substr(md5(strftime("%Y%m", time())), 0, 6);
			}
			$this->destination_subdir = trim($this->destination_subdir);
			if( strlen($this->destination_subdir) > 0 ) {
				if( !is_dir($this->destination . $this->destination_subdir . '/') ) {
					@mkdir($this->destination . $this->destination_subdir . '/');
				}
				if( !is_writable($this->destination . $this->destination_subdir . '/') ) {
					if( is_dir($this->destination . $this->destination_subdir . '/') ) {
						$this->_ms_add('top', array('destination_not_writable', $this->destination . $this->destination_subdir . '/'), 'error');
					} else {
						$this->_ms_add('top', array('destination_not_exist', $this->destination . $this->destination_subdir . '/'), 'error');
					}
					$has_error = true;
				}
			}
		}
		if( $has_error == true ) {
			return false;
		}
		return true;
	}
	
	function parseExtension() {
		if( $this->filename ) {
			$this->extension = strtolower(substr($this->filename, strrpos($this->filename, '.')+1));
		} else {
			$this->extension = strtolower(substr($this->file['name'], strrpos($this->file['name'], '.')+1));
		}
	}
	
	//SETTER/GETTER
	function setFile($file) {
		$this->file = $file;
	}
	
	function getFile() {
		return $this->file;
	}
	
	function set_rand_filename($is_rand = true) {
		if( $is_rand == true ) {
			$this->rand_filename = true;
		} else {
			$this->rand_filename = false;
		}
	}
	
	function setRandFilename($is_rand = true) {
		if( $is_rand == true ) {
			$this->rand_filename = true;
		} else {
			$this->rand_filename = false;
		}
	}
	
	function getRandFilename() {
		return $this->rand_filename;
	}
	
	function setExtension($extension) {
		$this->extension = $extension;
	}
	
	function getExtension() {
		return $this->extension;
	}
	
	function setDestination($destination) {
		$this->destination = $destination;
	}
	
	function getDestination() {
		return $this->destinaction;
	}
	
	function setDestinationType($destination_type) {
		$this->destination_type = $destination_type;
	}
	
	function getDestinationType() {
		return $this->destination_type;
	}
	
	function setDestinationSubDir($destination_subdir) {
		$this->destination_subdir = $destination_subdir;
	}
	
	function getDestinationSubDir() {
		return $this->destination_subdir;
	}
	
	function setPermissions($permissions) {
		$this->permissions = octdec($permissions);
	}
	
	function getPermissions() {
		return $this->permissions;
	}
	
	function setFilename($filename) {
		$this->filename = $filename;
	}
	
	function getFilename() {
		return $this->filename;
	}
	
	function setFiletype($filetype) {
		$this->filetype = $filetype;
	}
	
	function getFiletype() {
		return $this->filetype;
	}
	
	function setTmpFilename($filename) {
		$this->tmp_filename = $filename;
	}
	
	function getTmpFilename() {
		return $this->tmp_filename;
	}
	
	function getSaveFilename() {
		return $this->save_filename;
	}
	
	function setExtensions($extensions) {
		if( $this->_not_null($extensions) ) {
			if( is_array($extensions) ) {
				$this->extensions = $extensions;
			} else {
				$this->extensions = array($extensions);
			}
		} else {
			$this->extensions = array();
		}
	}
	
	function getExtensions() {
		return $this->extensions;
	}
	
	function setOutputMessages($location) {
		switch($location) {
			case 'session':
				$this->message_location = 'session';
				break;
			case 'direct':
			default:
				$this->message_location = 'direct';
				break;
		}
	}
	
	function getOutputMessages() {
		return $this->message_location;
	}
	
	function getMessages() {
		return $this->_messagestacks;
	}
	
	//private
	function _not_null($value) {
		return util_not_null($value);
	}
	
	function _random() {
		return util_create_random_value('8', 'mixed');
	}
	
	function _generate_filename() {
		//return md5(time()) . '.' . $this->extension;
		return substr(md5(time()), 0, 16) . '_' . $this->_random() . '.' . $this->extension;
	}
	
	function _filter_filename($filename = '', $check = true, $rand = false) {
		if( $filename == '' ) {
			$filename = $this->filename;
		}
		$name = $filename;
		$filename = substr($filename, 0, -(strlen($this->extension)+1));
		$filename = stripslashes($filename);
		$filename = trim(ereg_replace("[^A-Za-z0-9\-\_]", " ", $filename));
		$filename = ereg_replace("( {1,})", "-", $filename);
		$filename = trim(substr($filename, 0, 50));
		if( $rand == true ) {
			$filename .= '_' . $this->_random();
		}
		$filename .= '.' . $this->extension;
		
		if( $check == true ) {
			if( strlen($this->destination_subdir) > 0 ) {
				$savefile = $this->destination . $this->destination_subdir . '/' . $filename;
			} else {
				$savefile = $this->destination . '/' . $filename;
			}
			if( file_exists($savefile) ) {
				$filename = $this->_filter_filename($name, $check, true);
			}
		}
		
		return $filename;
	}
	
	function _ms_add($class, $message, $type = 'error') {
		global $messagestackObj;
		
		if( is_array($message) ) {
			$message = sprintf(_lang($message['0'], 'upload'), $message['1']);
		} else {
			$message = _lang($message, 'upload');
		}
		
		if( $this->message_location != 'direct' && is_object($messagestackObj) ) {
			$messagestackObj->add($class, $message, $type);
		} else {
			$this->_messagestacks[] = array('class' => $class, 'message' => $message, 'type' => $type);
		}
	}
	
	function _ms_add_session($class, $message, $type = 'error') {
		global $messagestackObj;
		
		if( is_array($message) ) {
			$message = sprintf(_lang($message['0'], 'upload'), $message['1']);
		} else {
			$message = _lang($message, 'upload');
		}
		
		if( $this->message_location != 'direct' && is_object($messagestackObj) ) {
			$messagestackObj->add_session($class, $message, $type);
		} else {
			$this->_messagestacks[] = array('class' => $class, 'message' => $message, 'type' => $type);
		}
	}
}

//MultiUpload
class CoreMultiUpload {
	var $file;
	var $filename;
	var $filetype;
	var $filesize;
	var $fileerror;
	var $tmp_filename;
	var $save_filename;
	var $save_file;
	var $extension;
	
	var $destination;
	var $permissions;
	var $extensions;
	var $types;
	
	var $maxsize = 0;
	var $message_location;
	
	var $destination_type = 'random';
	var $destination_subdir;
	var $rand_filename = true;
	
	//constructor
	function CoreMultiUpload($file = '', $destination = '', $permissions = '777', $extensions = '') {
		$this->setFile($file);
		//$this->parseExtension();
		
		$this->setDestination($destination);
		$this->setPermissions($permissions);
		$this->setExtensions($extensions);
		
		$this->setOutputMessages('direct');
		
		if( $this->_not_null($this->file) && $this->_not_null($this->destination)) {
			$this->setOutputMessages('session');
			
			if( ($this->parse() == true) && ($this->save() == true) ) {
				return true;
			} else {
				return false;
			}
		}
	}
	
	//methods
	function parse() {
		$has_error = false;
		
		if( substr($this->destination, -1) != '/' ) {
			$this->destination .= '/';
		}
		
		if( isset($_FILES[$this->file]) ) {
			$file = array('name' => $_FILES[$this->file]['name'],
						'type' => $_FILES[$this->file]['type'],
						'size' => $_FILES[$this->file]['size'],
						'tmp_name' => $_FILES[$this->file]['tmp_name']);
		} elseif( isset($GLOBALS['HTTP_POST_FILES'][$this->file]) ) {
			global $HTTP_POST_FILES;
		
			$file = array('name' => $HTTP_POST_FILES[$this->file]['name'],
						'type' => $HTTP_POST_FILES[$this->file]['type'],
						'size' => $HTTP_POST_FILES[$this->file]['size'],
						'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']);
		} else {
			$file = array('name' => (isset($GLOBALS[$this->file . '_name']) ? $GLOBALS[$this->file . '_name'] : ''),
						'type' => (isset($GLOBALS[$this->file . '_type']) ? $GLOBALS[$this->file . '_type'] : ''),
						'size' => (isset($GLOBALS[$this->file . '_size']) ? $GLOBALS[$this->file . '_size'] : ''),
						'tmp_name' => (isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] : ''));
		}
		
		if( is_array($file['tmp_name']) ) {
			foreach($file['tmp_name'] as $k => $v) {
				if( $this->_not_null($v) && ($v != 'none') && is_uploaded_file($v) ) {
					if( !empty($this->extensions) && !in_array(strtolower(substr($v, strrpos($v, '.')+1)), $this->extensions)) {
						$this->_ms_add('top', 'filetype_not_allowed', 'error');
						$has_error = true;
					}
				
					$this->setFile($file);
					$this->setFilename($file['name']);
					//$this->parseExtension($file['name'][$k]);
					$this->setFiletype($file['type']);
					$this->setFilesize($file['size']);
					$this->setTmpFilename($file['tmp_name']);
				} else {
					$this->_ms_add('top', 'no_file_uploaded', 'warning');
					//$has_error = true;
				}
			}
		} else {
			if( $this->_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) {
				if( !empty($this->extensions) && !in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions)) {
					$this->_ms_add('top', 'filetype_not_allowed', 'error');
					$has_error = true;
				}
			
				$this->setFile($file);
				$this->setFilename($file['name']);
				$this->parseExtension();
				$this->setFiletype($file['type']);
				$this->setFilesize($file['size']);
				$this->setTmpFilename($file['tmp_name']);
				
				if( $this->checkFile($file) == false ) {
					$has_error = true;
				}
			} else {
				$this->_ms_add('top', 'no_file_uploaded', 'warning');
				$has_error = true;
			}
		}
		if( $has_error == true ) {
			return false;
		}
		return $this->checkDestination();
	}
	
	function save() {
		if( is_array($this->file['tmp_name']) ) {
			foreach($this->file['tmp_name'] as $k => $v) {
				$this->parseExtension($this->file['name'][$k]);
				if( $this->rand_filename == true || strlen($this->filename[$k]) == 0 ) {
					$save_filename = $this->_generate_filename();
				} else {
					$save_filename = $this->filename[$k];
					$save_filename = $this->_filter_filename($save_filename);
				}
				
				if( strlen($this->destination_subdir) > 0 ) {
					$save_filename = $this->destination_subdir . '/' . $save_filename;
				}
				$savepath = $this->destination . $save_filename;
				
				if( move_uploaded_file($v, $savepath) ) {
					chmod($savepath, $this->permissions);
					
					$this->save_filename[] = $save_filename;
					$this->save_file[] = $savepath;
					
					$this->_ms_add('top', 'file_saved_success', 'success');
				} else {
					$this->save_filename[] = '';
					$this->save_file[] = '';
					
					$this->_ms_add('top', 'file_not_saved', 'error');
				}
			}
				
			return true;
		} else {
			if( $this->rand_filename == true || strlen($this->filename) == 0 ) {
				$this->save_filename = $this->_generate_filename();
			} else {
				//$this->save_filename = $this->filename;
				$this->save_filename = $this->_filter_filename($this->filename);
			}
			if( strlen($this->destination_subdir) > 0 ) {
				$this->save_filename = $this->destination_subdir . '/' . $this->save_filename;
			}
			$savepath = $this->destination . $this->save_filename;
			if( move_uploaded_file($this->file['tmp_name'], $savepath) ) {
				chmod($savepath, $this->permissions);
				
				$this->save_file = $savepath;
				$this->_ms_add('top', 'file_saved_success', 'success');
				return true;
			} else {
				$this->_ms_add('top', 'file_not_saved', 'error');
				return false;
			}
		}
	}
	
	function upload() {
		if( ($this->parse() == true) && ($this->save() == true) ) {
			return true;
		} else {
			return false;
		}
	}
	
	//
	function checkFile($file) {
		$extension = strtolower(substr($file['name'], strrpos($file['name'], '.')+1));
		$type = $file['type'];
		$size = $file['size'];
		if( $this->checkFileExt($extension) == false || $this->checkFileType($type) == false || $this->checkFileSize($size) == false ) {
			return false;
		}
		return true;
	}
	
	function checkFileExt($extension) {
		if( !empty($this->extensions) && !in_array(strtolower($extension), $this->extensions)) {
			$this->_ms_add('top', 'filetype_not_allowed', 'error');
			return false;
		}
		return true;
	}
	
	function checkFileType($type) {
		if( !empty($this->types) && !in_array(strtolower($type), $this->types) ) {
			$this->_ms_add('top', 'filetype_not_allowed', 'error');
			return false;
		}
		return true;
	}
	
	function checkFileSize($size) {
		if( $this->maxsize > 0 && $this->maxsize <= $size ) {
			$this->_ms_add('top', 'filesize_not_allowed', 'error');
			return false;
		}
		return true;
	}
	
	function checkFileUploadable($file) {
		return is_uploaded_file($file);
	}
	
	function checkDestination() {
		$has_error = false;
		if( !is_writable($this->destination) ) {
			if( is_dir($this->destination) ) {
				$this->_ms_add('top', array('destination_not_writable', $this->destination), 'error');
			} else {
				$this->_ms_add('top', array('destination_not_exist', $this->destination), 'error');
			}
			$has_error = true;
		} else {
			$this->destination_subdir = '';
			if( $this->destination_type == 'month' ) {
				$this->destination_subdir = strftime("%Y%m", time());
			} elseif( $this->destination_type == 'day' ) {
				$this->destination_subdir = strftime("%Y%m%d", time());
			} elseif( $this->destination_type == 'random' ) {
				$this->destination_subdir = substr(md5(strftime("%Y%m", time())), 0, 6);
			}
			$this->destination_subdir = trim($this->destination_subdir);
			if( strlen($this->destination_subdir) > 0 ) {
				if( !is_dir($this->destination . $this->destination_subdir . '/') ) {
					@mkdir($this->destination . $this->destination_subdir . '/');
				}
				if( !is_writable($this->destination . $this->destination_subdir . '/') ) {
					if( is_dir($this->destination . $this->destination_subdir . '/') ) {
						$this->_ms_add('top', array('destination_not_writable', $this->destination . $this->destination_subdir . '/'), 'error');
					} else {
						$this->_ms_add('top', array('destination_not_exist', $this->destination . $this->destination_subdir . '/'), 'error');
					}
					$has_error = true;
				}
			}
		}
		if( $has_error == true ) {
			return false;
		}
		return true;
	}
	
	function parseExtension($filename = '') {
		if( $filename == '' ) {
			if( $this->filename ) {
				$this->extension = strtolower(substr($this->filename, strrpos($this->filename, '.')+1));
			} else {
				if( !is_array($this->file['name']) ) {
					$this->extension = strtolower(substr($this->file['name'], strrpos($this->file['name'], '.')+1));
				}
			}
		} else {
			$this->extension = strtolower(substr($filename, strrpos($filename, '.')+1));
		}
	}
	
	//SETTER/GETTER
	function setFile($file) {
		$this->file = $file;
	}
	
	function getFile() {
		return $this->file;
	}
	
	function set_rand_filename($is_rand = true) {
		if( $is_rand == true ) {
			$this->rand_filename = true;
		} else {
			$this->rand_filename = false;
		}
	}
	
	function setRandFilename($is_rand = true) {
		if( $is_rand == true ) {
			$this->rand_filename = true;
		} else {
			$this->rand_filename = false;
		}
	}
	
	function getRandFilename() {
		return $this->rand_filename;
	}
	
	function setExtension($extension) {
		$this->extension = $extension;
	}
	
	function getExtension() {
		return $this->extension;
	}
	
	function setDestination($destination) {
		$this->destination = $destination;
	}
	
	function getDestination() {
		return $this->destinaction;
	}
	
	function setDestinationType($destination_type) {
		$this->destination_type = $destination_type;
	}
	
	function getDestinationType() {
		return $this->destination_type;
	}

	
	function setDestinationSubDir($destination_subdir) {
		$this->destination_subdir = $destination_subdir;
	}
	
	function getDestinationSubDir() {
		return $this->destination_subdir;
	}
	
	function setPermissions($permissions) {
		$this->permissions = octdec($permissions);
	}
	
	function getPermissions() {
		return $this->permissions;
	}
	
	function setFilename($filename) {
		$this->filename = $filename;
	}
	
	function getFilename() {
		return $this->filename;
	}
	
	function setFiletype($filetype) {
		$this->filetype = $filetype;
	}
	
	function getFilesize() {
		return $this->filesize;
	}
	
	function setFilesize($filesize) {
		$this->filesize = $filesize;
	}
	
	function getFiletype() {
		return $this->filetype;
	}
	
	function setTmpFilename($filename) {
		$this->tmp_filename = $filename;
	}
	
	function getTmpFilename() {
		return $this->tmp_filename;
	}
	
	function getSaveFilename() {
		return $this->save_filename;
	}
	
	function setExtensions($extensions) {
		if( $this->_not_null($extensions) ) {
			if( is_array($extensions) ) {
				$this->extensions = $extensions;
			} else {
				$this->extensions = array($extensions);
			}
		} else {
			$this->extensions = array();
		}
	}
	
	function getExtensions() {
		return $this->extensions;
	}
	
	function setOutputMessages($location) {
		switch($location) {
			case 'session':
				$this->message_location = 'session';
				break;
			case 'direct':
			default:
				$this->message_location = 'direct';
				break;
		}
	}
	
	function getOutputMessages() {
		return $this->message_location;
	}
	
	function getMessages() {
		return $this->_messagestacks;
	}
	
	//private
	function _not_null($value) {
		return util_not_null($value);
	}
	
	function _random() {
		return util_create_random_value('8', 'mixed');
	}
	
	function _generate_filename() {
		//return md5(time()) . '.' . $this->extension;
		return substr(md5(time()), 0, 16) . '_' . $this->_random() . '.' . $this->extension;
	}
	
	function _filter_filename($filename = '', $check = true, $rand = false) {
		if( $filename == '' ) {
			$filename = $this->filename;
		}
		$name = $filename;
		$filename = substr($filename, 0, -(strlen($this->extension)+1));
		$filename = stripslashes($filename);
		$filename = trim(ereg_replace("[^A-Za-z0-9\-\_]", " ", $filename));
		$filename = ereg_replace("( {1,})", "-", $filename);
		$filename = trim(substr($filename, 0, 50));
		if( $rand == true ) {
			$filename .= '_' . $this->_random();
		}
		$filename .= '.' . $this->extension;
		
		if( $check == true ) {
			if( strlen($this->destination_subdir) > 0 ) {
				$savefile = $this->destination . $this->destination_subdir . '/' . $filename;
			} else {
				$savefile = $this->destination . '/' . $filename;
			}
			if( file_exists($savefile) ) {
				$filename = $this->_filter_filename($name, $check, true);
			}
		}
		
		return $filename;
	}
	
	function _ms_add($class, $message, $type = 'error') {
		global $messagestackObj;
		
		if( is_array($message) ) {
			$message = sprintf(_lang($message['0'], 'upload'), $message['1']);
		} else {
			$message = _lang($message, 'upload');
		}
		
		if( $this->message_location != 'direct' && is_object($messagestackObj) ) {
			$messagestackObj->add($class, $message, $type);
		} else {
			$this->_messagestacks[] = array('class' => $class, 'message' => $message, 'type' => $type);
		}
	}
	
	function _ms_add_session($class, $message, $type = 'error') {
		global $messagestackObj;
		
		if( is_array($message) ) {
			$message = sprintf(_lang($message['0'], 'upload'), $message['1']);
		} else {
			$message = _lang($message, 'upload');
		}
		
		if( $this->message_location != 'direct' && is_object($messagestackObj) ) {
			$messagestackObj->add_session($class, $message, $type);
		} else {
			$this->_messagestacks[] = array('class' => $class, 'message' => $message, 'type' => $type);
		}
	}
}
//
?>